package com.jiutong.meipengdai.views.popwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiutong.meipengdai.R;

import retrofit2.Callback;

public abstract class BasePopupWindow {
    /**
     * 上下文对象
     */
    public Context mContext;
    /**
     * 容器视图
     */
    public RelativeLayout mContainer;

    /**
     * 弹出窗口
     */
    private PopupWindow mPopupWindow;

    private View llpop;

    //默认点击pop外面dismiss
    private boolean mIsTouchBoundDismiss = true;

    //设置透明图0-255 默认100
    private int mPopAlpha = 100;

    private int animStyle = R.style.AnimBottom;
    private boolean mFocusable = true;

    public BasePopupWindow(Context context) {
        this.mContext = context;

        mContainer = (RelativeLayout) LayoutInflater.from(mContext).inflate(
                getLayoutId(), null);

        llpop = getContentView();

        initView();
        initEvent();
    }

    public abstract void initView();

    public abstract void initEvent();

    public abstract View getContentView();

    /**
     * 显示弹出窗口
     *
     * @param parent  基于什么视图定位
     * @param grivate 定位在指定视图的什么位置
     */
    public void showPopupWindow(View parent, int grivate) {

        if (mIsTouchBoundDismiss) {
            mContainer.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {

                    int height = llpop.getTop();

                    int y = (int) event.getY();

                    if (event.getAction() == MotionEvent.ACTION_UP) {

                        if (y < height || y > llpop.getBottom()) {

                            mPopupWindow.dismiss();

                        }
                    }
                    return true;
                }
            });
        }
        mPopupWindow = new PopupWindow(mContainer, LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);

        mPopupWindow.setFocusable(mFocusable);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);

        // 设置SelectPicPopupWindow弹出窗体的背景
        mPopupWindow.setBackgroundDrawable(dw);
        mPopupWindow.getBackground().setAlpha(mPopAlpha);// 设置透明度0~255
        if (animStyle != 0)
            mPopupWindow.setAnimationStyle(animStyle);
        mPopupWindow.update();

        if (mPopupWindow != null) {
            if (mPopupWindow.isShowing()) {
                mPopupWindow.dismiss();
            }
//            } else if( Build.VERSION.SDK_INT < Build.VERSION_CODES.N){//Android 7.0以上showAsDropDown无效
            if (grivate != 0) {
                mContainer.setGravity(grivate);
            }
            mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
            mPopupWindow.showAsDropDown(parent);
//            }else{
//                //解决虚拟键盘遮挡的问题
//                mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
//                mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
//
//                int height = parent.getHeight();
//                int top = parent.getTop();
//                mPopupWindow.showAtLocation(parent, Gravity.NO_GRAVITY, 0, height + top);
//            }
        }
    }

    protected void loadData() {

    }

    public void setAnimationStyle(int style) {
        this.animStyle = style;
    }


    public abstract int getLayoutId();

    /**
     * 点击外面部分是否消失
     *
     * @param isTouchBoundDismiss
     */
    public void setTouchBoundDismiss(boolean isTouchBoundDismiss) {
        mIsTouchBoundDismiss = isTouchBoundDismiss;
    }

    public void setmPopAlpha(int alpha) {
        this.mPopAlpha = alpha;
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    public boolean isShowing() {
        return mPopupWindow == null ? false : mPopupWindow.isShowing();
    }

    public void setFocusable(boolean focusable) {
        this.mFocusable = focusable;
    }
}
